# 使用code获取access_token
from mypro import settings
import requests
def get_user_info(code):
    token_data = {
        'grant_type': 'authorization_code',
        'code': code,
        'client_id': settings.GITEE_OAUTH_CONFIG['client_id'],
        'client_secret': settings.GITEE_OAUTH_CONFIG['client_secret'],
        'redirect_uri': settings.GITEE_OAUTH_CONFIG['redirect_uri']
    }

    token_response = requests.post(
        settings.GITEE_OAUTH_CONFIG['token_url'],
        data=token_data
    )
    token_response.raise_for_status()
    token_json = token_response.json()

    if 'access_token' not in token_json:
        raise ValueError('Failed to get access token')

    access_token = token_json['access_token']

    user_response = requests.get(
        settings.GITEE_OAUTH_CONFIG['user_url'],
        params={'access_token': access_token}
    )
    user_response.raise_for_status()
    user_info = user_response.json()
    return user_info
def get_auth_url(state):
    auth_url = (
        f"{settings.GITEE_OAUTH_CONFIG['auth_url']}?"
        f"client_id={settings.GITEE_OAUTH_CONFIG['client_id']}&"
        f"redirect_uri={settings.GITEE_OAUTH_CONFIG['redirect_uri']}&"
        f"response_type=code&"
        f"state={state}"
    )
    return auth_url